// pages/testapp/testapp.js
const image = require("./image.js").image
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  printdate(e) {
    console.log(`[${new Date()}]`)
  },

  testhook1(e) {
    console.log(e);
    wx.getSystemInfo({
      success: (result) => {
        console.log(result)
      }
    })
  },

  testhook2(e) {
    wx.getSystemInfo().then(d => console.log('promise', d));
  },

  testhook3(e) {
    wx.getSystemInfoSync();
  },

  testpagehook(e) {
    console.log(this.data.motto);
  },

  setEnableDebug() {
    console.log(wx.setEnableDebug)
    wx.setEnableDebug({
      enableDebug: true,
    })
  },

  wxenv(e) {
    let s = ""
    Object.entries(wx.env).forEach(([k, v]) => {
      console.log(k)
      s += `${k}: ${v}\n`
    })
    wx.showModal({
      content: s
    })
  },

  chooseMedia(res) {
    wx.chooseMedia({
      success: (res) => {this.setData({imgSrc: res.tempFiles[0].tempFilePath})},
      fail: console.log
    })
  },
  chooseImage(res) {
    wx.chooseImage({
      success: (res) => {this.setData({imgSrc: res.tempFilePaths[0]})},
      fail: console.log
    })
  },

  testImage(e) {
    // const raw = wx.base64ToArrayBuffer(image)
    const fs = wx.getFileSystemManager()
    const filePath = this.getTmpPath(`${Date.now()}.png`)
    fs.writeFile({
      filePath: filePath,
      data: image,
      encoding: "base64",
      success: (res) => {
        console.log("src len", image.length, res)
        fs.stat({
          path: filePath,
          success: (res) => {
            this.mockAndChoose(filePath, res.stats.size)
          }
        })
        
        // wx.saveImageToPhotosAlbum({
        //   filePath: filePath,
        //   complete: console.log
          
        // })


      },
      fail: console.error
    })
    
  },

  mockAndChoose(filePath, size) {
    const oriChooseImage = wx.chooseImage
    Object.defineProperty(wx, "chooseImage", {
      writable: true,
      value(obj) {
        
          obj.success && obj.success({
            errMsg: "chooseImage:ok",
            tempFilePaths: [filePath],
            tempFiles: [
              {path: filePath, size: size}
            ]
          })
          wx.chooseImage = oriChooseImage
        
      }
    })
    wx.chooseImage({
      success: (res) => {
        console.log("choose", res)
        this.setData({
          imgSrc: res.tempFilePaths[0]
        })
      }
    })
  },

  getTmpPath(filename) {
    return `${wx.env.USER_DATA_PATH}/${filename}`
    if (wx.env.USER_DATA_PATH.startsWith("http://")) {
      //dev tool
      return `http://tmp/${filename}`
    } else {
      return `wxfile://tmp_${filename}`
    }
  },

  storage() {
    wx.setStorage({key: "a", data: Date.now(), success: () => {
      wx.getStorage({
        key: "a",
        success: console.log
      })
    }})
  },

  onGyroscopeChange() {
    wx.startGyroscope({
      interval: 2000,
    })
    wx.onGyroscopeChange(({x, y, z}) => {
      console.log(`x: ${x}, y: ${y}, z: ${z}, `)
    })
  },

  navigateToMiniProgram(e) {
    wx.navigateToMiniProgram({
      appId: "wx3eb9cfc5787d5458",
      path: "pages/testpage/testpage?ddd=1",
      envVersion: 'develop',
      complete(e) {
        console.warn(e)
      }
    })
  },

  pagerouterjump(e) {
      console.warn("__page_router_rewrited__", this.pageRouter.__page_router_rewrited__)
    this.pageRouter.navigateTo({
        url: './jumptestpage?a=1'
      })
  }
})
